using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

using InformationSchema;

namespace Core
{
    public class Column
    {
        #region fields
        // 1
        private string table_schema = string.Empty;
        // 2
        private string table_name = string.Empty;
        // 3
        private string name = string.Empty;
        // 6
        private string is_nullable;
        // 7
        private string data_type = string.Empty;
        // 8
        private long character_maximum_length;
        // 9
        private long character_octect_lenght;
        // 10
        private long numeric_precision;
        // 11
        private long numeric_scale;
        // 14
        private string column_type = string.Empty;
        // 15
        private string column_key = string.Empty;
        // 18
        private string column_comment = string.Empty;

        #endregion

        #region properties
        public string TableSchema
        {
            get { return table_schema; }
        }

        public string TableName
        {
            get { return table_name; }
        }

        public string Name
        {
            get { return name; }
        }

        public string IsNullable
        {
            get { return is_nullable; }
        }

        public string DataType
        {
            get { return data_type; }
        }

        public long NumericPrecision
        {
            get { return numeric_precision; }
        }

        public long NumericScale
        {
            get { return numeric_scale; }
        }

        public string ColumnType
        {
            get { return column_type; }
        }

        public string ColumnKey
        {
            get { return column_key; }
        }

        public string ColumnComment
        {
            get { return column_comment; }
        }

        public long CharacterMaximumLength
        {
            get { return character_maximum_length; }
        }

        public long CharacterOctectLength
        {
            get { return character_octect_lenght; }
        }

        #endregion

        private static Column CreateFromReader(DbDataReader reader)
        {
            Column column = new Column();

            column.table_schema = reader.GetString(1);
            column.table_name = reader.GetString(2);
            column.name = reader.GetString(3);

            if (!reader.IsDBNull(6)) column.is_nullable = reader.GetString(6);

            if (!reader.IsDBNull(7)) column.data_type = reader.GetString(7);

            if (!reader.IsDBNull(8)) column.character_maximum_length = reader.GetInt64(8);
            if (!reader.IsDBNull(9)) column.character_octect_lenght = reader.GetInt64(9);

            if (!reader.IsDBNull(10)) column.numeric_precision = reader.GetInt64(10);
            if (!reader.IsDBNull(11)) column.numeric_scale = reader.GetInt64(11);

            column.column_type = reader.GetString(14);

            if (!reader.IsDBNull(15)) column.column_key = reader.GetString(15);
            if (!reader.IsDBNull(18)) column.column_comment = reader.GetString(18);

            return column;
        }

        public static List<Column> FindAll()
        {
            string sql = "SELECT * FROM information_schema.`COLUMNS` C";
            List<Column> columns = new List<Column>();

            using (DbDataReader reader = DatabaseHelper.ExecuteReader(sql))
            {
                while (reader.Read())
                {
                    columns.Add(CreateFromReader(reader));
                }
            }

            return columns;
        }
    }
}
